package model.roomManagement.Implementation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.StringTokenizer;


	 class Client extends Thread { 
		  Socket socket;//连接端口 
		  
		  int userId ;//用户id 
		  String userName ;//用户姓名 
		  String ip; //客户端ip地址 
		  int userRoomId=0;
		  InetSocketAddress audioInetSocketAddress;//语音地址




		BufferedReader reader;//输入流 
		  PrintStream ps;//输出流 
		  int lostTime=0;
		  
		  
		  
		  
		  public Client(Socket s) { 
		   socket = s; 
		   try { 
		    reader = new BufferedReader(new InputStreamReader(s.getInputStream()));//得到输入流 
		    ps = new PrintStream(s.getOutputStream());//得到输出流 
		    String info = reader.readLine();//读取接收到的信息 
		    StringTokenizer stinfo = new StringTokenizer(info,":"); //分解字符串 
		   
		
		
		    ip = ((s.getInetAddress()).getHostAddress()); //获取IP地址  String类型
		   
		    audioInetSocketAddress=new InetSocketAddress(ip,5000);
		    
		    ///////////////////
		    System.out.println(ip+"连接\n");//得到客户机地址 
		    
		    stinfo.nextToken();//去掉“info”
		    if (stinfo.hasMoreTokens()){ 
			     userName = stinfo.nextToken() ;//获取name
			    } 
		    if (stinfo.hasMoreTokens()){ 
		     userId = Integer.parseInt(stinfo.nextToken()) ;//获取id 
		    } 
		   } catch (IOException ex) { 
		    ex.printStackTrace(); 
		   } 
		/////////////////
		   System.out.println("----clentInfo---- "); 
		   System.out.println("userName:"+ userName ); 
		   System.out.println("userId"+ userId ); 
		   System.out.println("userRoomId"+ userRoomId ); 
		   System.out.println("userIp"+ ip ); 
		   StringBuffer updataIp=new StringBuffer("updataIp");
		   updataIp.append(":"+ip);
		   send(updataIp);
		   
		  } 	  
		  
		  

		  
		  
    //**************客户端 run
		  public void run() {
			  Thread time=new Thread(new Timer(this));
			  time.start();
			  
		   while (true) { 
		    String line = null; 
		    try { 
		      
		     line = reader.readLine(); 
		 ///////////    
		//     System.out.println("line:"+line); 
		      
		    } catch (Exception ex) { 
		   //  ex.printStackTrace(); //输出错误信息      ChatServer.disconnect(this);//断开连接 
		     Server.disconnect(this); //异常断开
		     Server.notifyChatRoom(userRoomId);//更新信息 
		     return ; 
		    } 
		    if (line.endsWith("HeartTest")){
		    	lostTime=0;
		    /////////////
		    	  //System.out.println("HeartTest"); 
		    //  return;
		    }
		    if (line == null) { //客户离开 
		     Server.disconnect(this); 
		     Server.notifyChatRoom(userRoomId); 
		     return;
		    } 
		    StringTokenizer st = new StringTokenizer(line,":");//分解字符串 
		    String keyword = st.nextToken(); 
		    if(keyword.equals("roomId")){
		    	
			  System.out.println("ss:"+audioInetSocketAddress.getPort());
			    String roomId = st.nextToken(); 
	    	    userRoomId=Integer.parseInt(roomId);
	    	///////////////////    
	    	    Server.notifyEnter(ip, userRoomId);
	    	    Server.notifyChatRoom(userRoomId); 
		    }
		    else if(keyword.equals("exitRoom")){
		    	Server.notifyLeave(ip, userRoomId);
		    	int temp=userRoomId;
		    	userRoomId=0;
		    	Server.notifyChatRoom(temp); 
		    }
		    else if (keyword.equals("quit")) { //退出命令 
		     Server.disconnect(this); //断开连接 
		     if(userRoomId!=0)
		    	 Server.notifyLeave(ip, userRoomId);
		     Server.notifyChatRoom(userRoomId); //刷新信息 
		     return ;
		    } 
		   } 
		  } 
		  
		public void send (StringBuffer msg) { 
			   ps.println(msg); //输出信息 
			   ps.flush(); 
			  } 
		 } 